import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);
//登录导航页
const LoginBanner = resolve => {
  require.ensure(["@/components/LoginBanner.vue"], () => {
    resolve(require("@/components/LoginBanner.vue"));
  });
};
//主页-框架页
const Home = resolve => {
  require.ensure(["@/components/Home.vue"], () => {
    resolve(require("@/components/Home.vue"));
  });
};
//首页
const Main = resolve => {
  require.ensure(["@/components/Main.vue"], () => {
    resolve(require("@/components/Main.vue"));
  });
};
//数据源详情
const DataSource = resolve => {
  require.ensure(["@/components/datasourceDetail.vue"], () => {
    resolve(require("@/components/datasourceDetail.vue"));
  });
};
//工程管理
const Project = resolve => {
  require.ensure(["@/components/Project.vue"], () => {
    resolve(require("@/components/Project.vue"));
  });
};
//工程管理
const ProjectTwo = resolve => {
  require.ensure(["@/components/ProjectTwo.vue"], () => {
    resolve(require("@/components/ProjectTwo.vue"));
  });
};
//工程管理内容页
const FlowEditorContent = resolve => {
  require.ensure(["@/components/project/FlowEditorContent.vue"], () => {
    resolve(require("@/components/project/FlowEditorContent.vue"));
  });
};
//系统组件
const SystemComponent = resolve => {
  require.ensure(["@/components/SystemComponent.vue"], () => {
    resolve(require("@/components/SystemComponent.vue"));
  });
};

//菜单切换过渡页
const Transition = resolve => {
  require.ensure(["@/components/Transition.vue"], () => {
    resolve(require("@/components/Transition.vue"));
  });
};
// 数据库详情
// const DataSourceDetail = resolve => {
//   require.ensure(["@/components/dataSourceDetail.vue"], () => {
//     resolve(require("@/components/dataSourceDetail.vue"));
//   });
// };
// 源数据管理
const SourceDataMan = resolve => {
  require.ensure(["@/components/sourceDataMan.vue"], () => {
    resolve(require("@/components/sourceDataMan.vue"));
  });
};
// 数据集详情
const sourceDataDetail = resolve => {
  require.ensure(["@/components/sourceDataDetail.vue"], () => {
    resolve(require("@/components/sourceDataDetail.vue"));
  });
};
// 数据源管理
const dataSourceManagement = resolve => {
  require.ensure(["@/components/data_source/dataSourceMan.vue"], () => {
    resolve(require("@/components/data_source/dataSourceMan.vue"));
  });
};
// 数据集管理
const datasetManagement = resolve => {
  require.ensure(["@/components/data_source/dataSet.vue"], () => {
    resolve(require("@/components/data_source/dataSet.vue"));
  });
};
// 模型管理
const modelManagement = resolve => {
  require.ensure(["@/components/data_source/modelMan.vue"], () => {
    resolve(require("@/components/data_source/modelMan.vue"));
  });
};
// 日志管理
const journalManagement = resolve => {
  require.ensure(["@/components/data_source/logMan.vue"], () => {
    resolve(require("@/components/data_source/logMan.vue"));
  });
};

//基础配置
const baseSetting = resolve => {
  require.ensure(["@/components/baseSetting/index.vue"], () => {
    resolve(require("@/components/baseSetting/index.vue"));
  });
};

const routes = [
  { path: "", redirect: "/login_banner" },
  { path: "/login_banner", name: "LoginBanner", component: LoginBanner },
  {
    path: "/home",
    component: Home,
    children: [
      { path: "", redirect: "main", name: "Home" },
      { path: "trans", component: Transition }, //过渡页
      {
        path: "main",
        name: "Main",
        component: Main,
        children: [
          {
            path: "",
            name: "Project",
            component: FlowEditorContent,
            meta: { keepAlive: true }
          }
        ],
        meta: { keepAlive: true }
      },
      {
        path: "datasource",
        name: "DataSource",
        component: DataSource
      },
      {
        path: "administration",
        name: "Administration",
        component: dataSourceManagement
      },
      {
        path: "dataSet",
        name: "DataSet",
        component: datasetManagement
      },
      {
        path: "modelManagement",
        name: "ModelManagement",
        component: modelManagement
      },
      {
        path: "journalManagement",
        name: "JournalManagement",
        component: journalManagement
      },
      {
        path: "datasource-detail",
        name: "datasourceDetail",
        component: DataSource
      },
      {
        path: "sourceData-detail",
        name: "sourceDataDetail",
        component: sourceDataDetail
      },
      {
        path: "source-data-man",
        name: "sourceDataMan",
        component: SourceDataMan
      },
      {
        path: "project",
        component: Project,
        children: [{ path: "", name: "Project", component: FlowEditorContent }]
      },
      {
        path: "projectTwo",
        component: ProjectTwo,
        children: [
          { path: "", name: "ProjectTwo", component: FlowEditorContent }
        ]
      },
      {
        path: "components",
        name: "SystemComponent",
        component: SystemComponent
      },
      {
        path: "baseSetting",
        name: "baseSetting",
        component: baseSetting
      }
    ]
  },
  { path: "/show", name: "FlowEditorContent", component: FlowEditorContent }
];
const router = new Router({
  mode: "history",
  routes: routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    //如果未匹配到路由
    from.name ? next({ name: from.name }) : next("/"); //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  } else {
    next(); //如果匹配到正确跳转
  }
});

export default router;
